%***********************************************************
% CS 229 Machine Learning
% Project, Crop Lecturer
%-----------------------------------------------------------
% Date : November 13, 2010
%***********************************************************


%**************************************************************************
%Input parameters
outputwidth = 320;
outputheight = 180;
Wmax = 640;
Hmax = 360;
width = 640;
height = 360;
yuv_filename = '2010_EE261_L23_slice0.yuv';
numFrame = 10000;

lecturer_tracking_scale_factor = 640/2560;

load groundTruth.dat;
load lecturer.dat;
load panning.mat; %panning ground truth
%**************************************************************************

%lecuturer feature adjustment
lecturer = lecturer * lecturer_tracking_scale_factor;
lecturer(:,2) = min(max(lecturer(:,2),1),Wmax) ;
lecturer(:,3) = min(max(lecturer(:,3),1),Hmax) ;

% Lecturer cropping coordinate

lecturer_blob_width = 45;
lecturer_blob_height= 75;
lecturer_blob_xmin = min(max(floor(lecturer(:,2) - lecturer_blob_width/2),1),Wmax-lecturer_blob_width);
lecturer_blob_ymin = min(max(floor(lecturer(:,3) - lecturer_blob_height/2),1),Hmax-lecturer_blob_height);
blob_size = (lecturer_blob_width+1)*(lecturer_blob_height+1);

trainin_Seq = 20000:30:numFrame;

NON_PAN = zeros(blob_size,sum(panning_indicator(trainin_Seq) == 1));
PAN = zeros(blob_size,sum(panning_indicator(trainin_Seq) == 0));

NON_PAN_IND = 1;
PAN_IND = 1;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Crop Lecturer %%%%%%%%%%%%%%%%%%%%%%%%%%

for frame_number=trainin_Seq

 if(panning_indicator(frame_number) == 1) %panning 
    [Y, U, V] = yuvread(yuv_filename, width, height, frame_number,1);
    lecturer_blob = imcrop(Y,[lecturer_blob_xmin(frame_number) lecturer_blob_ymin(frame_number)  lecturer_blob_width lecturer_blob_height]);
    PAN(:,PAN_IND) = reshape(lecturer_blob,blob_size,1); %save blob as feature vector
    PAN_IND = PAN_IND +1;
    
%     figure(1)
%     imshow(uint8(lecturer_blob));
   
    
%     pause(1/200);
 else  %non-panning
     [Y, U, V] = yuvread(yuv_filename, width, height, frame_number,1);
    lecturer_blob = imcrop(Y,[lecturer_blob_xmin(frame_number) lecturer_blob_ymin(frame_number)  lecturer_blob_width lecturer_blob_height]);
    NON_PAN(:,NON_PAN_IND) = reshape(lecturer_blob,blob_size,1); %save blob as feature vector
    NON_PAN_IND = NON_PAN_IND +1;
%     figure(2)
%     imshow(uint8(lecturer_blob));
%     pause(1/200);    
    
 end

end

%save panning and non-panning gestures
save(['NON_PAN.mat'],'NON_PAN');
save(['PAN.mat'],'PAN');

load NON_PAN.mat
load PAN.mat

%debug input eigen gesture
for i=1:20
   figure(99)
   subplot(5,4,i)
   trainGesture = reshape(PAN(:,i),lecturer_blob_height+1,lecturer_blob_width+1);
   imagesc(uint8(trainGesture));  
   colormap(gray(256));
end


%%%%%%%%%PCA Analysis%%%%%%%%%%%%%%%%%%
%Panning eigengesture

[eVectors_PAN,eValues_PAN,Psi_PAN] = pca_evectors(double(PAN),25);
%And to explore the eigenvalue spectrum and how much variance the first n vectors account for, try the following:
figure(3)
plot(eValues_PAN);                       % To plot the eigenvalues
CVals = zeros(1,length(eValues_PAN));    % Allocate a vector same length as Vals
CVals(1) = eValues_PAN(1);
for i = 2:length(eValues_PAN)            % Accumulate the eigenvalue sum
     CVals(i) = CVals(i-1) + eValues_PAN(i);
end;
CVals = CVals / sum(eValues_PAN);        % Normalize total sum to 1.0
figure(4)
plot(CVals);                      % Plot the cumulative sum
ylim([0 1]);                      % Set Y-axis limits to be 0-1

for i=1:20
   figure(5)
   title(' panning eigengesture');
   eigenGesture = reshape(eVectors_PAN(:,i),lecturer_blob_height+1,lecturer_blob_width+1);
   subplot(5,4,i)
   imagesc((eigenGesture));  
   colormap(gray(256));
end
eigenGestures_Panning = eVectors_PAN ;
save(['eigenGestures_Panning.mat'],'eigenGestures_Panning');

%%%%%%%%%% non-panning eigengesture %%%%%%%%%%%%%%%%%%
[eVectors_NON_PAN,eValues_NON_PAN,Psi_NON_PAN] = pca_evectors(double(NON_PAN),25);
%And to explore the eigenvalue spectrum and how much variance the first n vectors account for, try the following:
figure()
plot(eValues_NON_PAN);                       % To plot the eigenvalues
CVals = zeros(1,length(eValues_NON_PAN));    % Allocate a vector same length as Vals
CVals(1) = eValues_NON_PAN(1);
for i = 2:length(eValues_NON_PAN)            % Accumulate the eigenvalue sum
     CVals(i) = CVals(i-1) + eValues_NON_PAN(i);
end;
CVals = CVals / sum(eValues_NON_PAN);        % Normalize total sum to 1.0
figure(4)
plot(CVals);                      % Plot the cumulative sum
ylim([0 1]);                      % Set Y-axis limits to be 0-1

for i=1:20
   figure(6)
   title('Non panning eigengesture');
   eigenGesture = reshape(eVectors_NON_PAN(:,i),lecturer_blob_height+1,lecturer_blob_width+1);
   subplot(5,4,i)
   imagesc((eigenGesture));  
   colormap(gray(256));
end
eigenGestures_NonPanning = eVectors_NON_PAN ;
save(['eigenGestures_NonPanning.mat'],'eigenGestures_NonPanning');

